import datetime
import os
import platform
import stat
import tarfile

from mailMessage import mailMessage
from options import options
from backupJob import backupJob

# Lecture de la configuration.
if "WINDOWS" == platform.system().upper():
	optionFile = options(fileName="C:\\SCRIPTS\\BACKUP\\backup.cfg")
else:
	optionFile = options(fileName="backup.cfg")

# Initialisation de l'email.
message = mailMessage(optionFile.getBool("GENERAL", "DEBUG_MODE"))
message.write("Demarrage de la procedure de sauvegarde.")

nbBackup = optionFile.getInt("GENERAL", "NUMBER")
for backup in range(0, nbBackup,):
	backupName = optionFile.getBackupString(backup, "NAME")
	initialFolder = optionFile.getBackupString(backup, "INITIAL_FOLDER")
	destinationFolder = optionFile.getBackupString(backup, "DESTINATION_FOLDER")
	retentionDuration = optionFile.getBackupInt(backup, "RETENTION_DAYS", 30)
	message.write("SAUVEGARDE %d: Copie des fichiers %s"%(backup, backupName,))
	backupItem = backupJob(message, backupName, initialFolder=initialFolder, destinationFolder=destinationFolder,)
	backupItem.run()
	message.write("SAUVEGARDE %d: FIN"%backup)
	message.write("=======================================================")

# Envoi du mail resume.
message.setSubject(optionFile.getString("MAILING", "TITLE"))
message.setFrom(optionFile.getString("MAILING", "FROM"))
message.setTo(optionFile.getString("MAILING", "TO"))
message.setSMTP(optionFile.getString("MAILING", "SMTP"))
mailListUsersStr = optionFile.getString("MAILING", "LIST_USERS")
message.write("Fin de la sauvegarde.")
message.sendMessage(mailListUsersStr.split(","))
